package com.example.villagemanager.ui.fragment.setting;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.example.villagemanager.R;
import com.example.villagemanager.VillageManagerApp;
import com.example.villagemanager.listener.OnDeviceSearchListener;
import com.example.villagemanager.model.event.Event;
import com.example.villagemanager.ui.adapter.BlueToothDeviceAdapter;
import com.example.villagemanager.ui.base.LazyloadFragment;
import com.example.villagemanager.util.bluetooth.BlueToothUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BlueToothSettingFragment extends LazyloadFragment {
    protected static final String TAG = "BlueToothSettingFragment";
    @BindView(R.id.open_switch)
    SwitchCompat openSwitch;
    @BindView(R.id.search_switch)
    SwitchCompat searchSwitch;
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;
    BlueToothUtil blueToothUtil;

    List<BluetoothDevice> deviceList=new ArrayList<>();
    BlueToothDeviceAdapter adapter;
    @Override
    protected int setContentView() {
        return R.layout.fragment_blue_tooth_setting;
    }

    @Override
    protected void init() {
        blueToothUtil= VillageManagerApp.blueToothUtil;
        blueToothUtil.initBluetooth();
        blueToothUtil.initBtBroadcast(new OnDeviceSearchListener() {
            @Override
            public void onDiscoveryStart() {

            }

            @Override
            public void onDiscoveryStop() {
                Log.d(TAG, "扫描到"+deviceList.size()+"台设备：" );
            }

            @SuppressLint("MissingPermission")
            @Override
            public void onDeviceFound(BluetoothDevice bluetoothDevice, int rssi) {
                deviceList.add(bluetoothDevice);
                Log.d(TAG, "扫描到设备：" + bluetoothDevice.getName() + "-->" + bluetoothDevice.getAddress());
                adapter.notifyDataSetChanged();
            }
        });
        adapter=new BlueToothDeviceAdapter(this.getContext(),deviceList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
        recyclerView.setAdapter(adapter);
        blueToothUtil.searchBtDevice();




    }

    @Override
    protected void lazyLoad() {

    }

    @SuppressLint("MissingPermission")
    @Override
    protected void onEventProcess(Event event) {


    }

    @Override
    public void InitLazy() {

    }
    @Override
    public void onDestroy() {
        super.onDestroy();

        //注销广播接收
        blueToothUtil.unregisterReceiver();
    }
}
